﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
namespace Exact_Extension
{
    public partial class SpecialDiscount : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        public SpecialDiscount(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.LoadInitializeData();
        }
        private void LoadInitializeData()
        {
            var querySchoolYear = from SCHY in DS.ISHCMC_SchoolYears
                                  select SCHY;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySchoolYear;
            this.lookUpEdit_SchoolYear.ItemIndex = 0;
        }

        private void LoadData()
        {
            List<ISHCMC_Invoice> ListInvoice = new List<ISHCMC_Invoice>();
            var queryInv = from Invoices in DS.ISHCMC_Invoices
                           where Invoices.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                           && Invoices.Itemcode == "TF" && Invoices.Authorize == false || !Invoices.Authorize.HasValue
                           select Invoices;
            foreach (ISHCMC_Invoice itemInv in queryInv)
            {
                itemInv.SPD_StartDate = DateTime.Now.Date;
                itemInv.SPD_EndDate = DateTime.Now.Date;
                ListInvoice.Add(itemInv);
            }
            this.iSHCMCInvoiceBindingSource.DataSource = ListInvoice;
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.LoadData(); 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ISHCMC_Invoice itemInv = (ISHCMC_Invoice)this.gridView_SpecialDiscount.GetRow(this.gridView_SpecialDiscount.GetSelectedRows()[0]);

            ISHCMC_Invoice itemInvoiceUpdate = DS.ISHCMC_Invoices.Single(c => c.ID == itemInv.ID);

            itemInvoiceUpdate.Discount = itemInv.Discount;
            itemInvoiceUpdate.SpecialDiscount = itemInv.SpecialDiscount;
            itemInvoiceUpdate.SPD_StartDate = itemInv.SPD_StartDate;
            itemInvoiceUpdate.SPD_EndDate = itemInv.SPD_EndDate;
            DS.SubmitChanges();
            this.LoadData();

        }
    }
}